<?php


require_once("login/src/Model/UserInfo.php");
require_once("Exercises/Model/MuscleGroup.php");
require_once("Database/Model/DatabaseConnection.php");


class MuscleGroupDAL
{
    /**
    *@var DatabaseConnection
    **/
	private $databaseConnection;

	public function __construct()
	{
		$this->databaseConnection = new DatabaseConnection();
	}

     /**
    *@return array MuscleGroupList
    **/
	public function getMuscleGroupList()
	{ 
		$sql = "SELECT `MuscleGroupID`, Name FROM mousclegroups";

		return $this->MuscleGroupList($sql);
	}

    /**
    *@param string sql
    *@return array MuscleGroupList
    **/
	private function MuscleGroupList($sql)
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);


		$stmt = $mysqli->prepare($sql);
                if ($stmt == FALSE) 
                {
                //throw new \Exception("prepare of [$sql] failed " . $this->mysqli->error);
                	echo "An error has occured:: ExerciseDAL1";
                }

                
                $result = $stmt->execute();
                if ($result == FALSE) 
                {
                    //throw new \Exception("execute of [$sql] failed " . $stmt->error);
                	echo "An error has occured:: ExerciseDAL2";
                }

                $result = $stmt->bind_result($muscleGroupID, $name);
                if ($result == FALSE) 
                {
                      //  throw new \Exception("execute of [$sql] failed " . $stmt->error);
                	echo "An error has occured:: ExerciseDAL3";
                }

                $muscleGroupList = array();

	            while ($stmt->fetch()) 
                {
	                $muscleGroupList[] = new MuscleGroup($muscleGroupID, $name);
	            }

	            $mysqli->close();
                return $muscleGroupList;
	}

}